/*   
 * Copyright (c) 2010-2020 DMGIS Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * DMGIS. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with DMGIS.   
 *   
 */
package com.dmgis.qxfw.dataservice.businessservice.common.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {
	/**
	 * 针对高度与宽度进行等比缩放
	 *
	 * @param img
	 * @param maxSize
	 *            要缩放到的尺寸
	 * @param type
	 *            1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放
	 * @return
	 */
	private static Image getScaledImage(BufferedImage img, int maxSize, int type) {
		int w0 = img.getWidth();
		int h0 = img.getHeight();
		int w = w0;
		int h = h0;
		if (type == 1) {
			w = w0 > h0 ? maxSize : (maxSize * w0 / h0);
			h = w0 > h0 ? (maxSize * h0 / w0) : maxSize;
		} else if (type == 2) {
			w = w0 > h0 ? (maxSize * w0 / h0) : maxSize;
			h = w0 > h0 ? maxSize : (maxSize * h0 / w0);
		}
		Image image = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
		BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = result.createGraphics();
		g.drawImage(image, 0, 0, null);// 在适当的位置画出
		return result;
	}

	/**
	 * 先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像
	 *
	 * @param img
	 * @param size
	 * @return
	 */
	public static BufferedImage getRoundedImage(BufferedImage img, int size) {
		return getRoundedImage(img, size, size / 2, 2);
	}

	/**
	 * 先按最小宽高为size等比例绽放, 然后图像居中抠出半径为radius的圆形图像
	 *
	 * @param img
	 * @param size
	 *            要缩放到的尺寸
	 * @param radius
	 *            圆角半径
	 * @param type
	 *            1:高度与宽度的最大值为maxSize进行等比缩放 , 2:高度与宽度的最小值为maxSize进行等比缩放
	 * @return
	 */
	private static BufferedImage getRoundedImage(BufferedImage img, int size, int radius, int type) {

		BufferedImage result = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = result.createGraphics();

		// 先按最小宽高为size等比例绽放, 然后图像居中抠出直径为size的圆形图像
		Image fixedImg = getScaledImage(img, size, type);
		g.drawImage(fixedImg, (size - fixedImg.getWidth(null)) / 2, (size - fixedImg.getHeight(null)) / 2, null);// 在适当的位置画出

		// 圆角
		if (radius > 0) {
			RoundRectangle2D round = new RoundRectangle2D.Double(0, 0, size, size, radius * 2, radius * 2);
			Area clear = new Area(new Rectangle(0, 0, size, size));
			clear.subtract(new Area(round));
			g.setComposite(AlphaComposite.Clear);

			// 抗锯齿
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g.fill(clear);
			g.dispose();
		}
		return result;
	}

	/**
	 * 使用删除alpha值的方式去掉图像的alpha通道
	 *
	 * @param $image
	 * @return
	 */
	protected static BufferedImage get24BitImage(BufferedImage $image) {
		int __w = $image.getWidth();
		int __h = $image.getHeight();
		int[] __imgARGB = getRGBs($image.getRGB(0, 0, __w, __h, null, 0, __w));
		BufferedImage __newImg = new BufferedImage(__w, __h, BufferedImage.TYPE_INT_RGB);
		__newImg.setRGB(0, 0, __w, __h, __imgARGB, 0, __w);
		return __newImg;
	}

	/**
	 * 使用绘制的方式去掉图像的alpha值
	 *
	 * @param $image
	 * @param $bgColor
	 * @return
	 */
	protected static BufferedImage get24BitImage(BufferedImage $image, Color $bgColor) {
		int $w = $image.getWidth();
		int $h = $image.getHeight();
		BufferedImage img = new BufferedImage($w, $h, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = img.createGraphics();
		g.setColor($bgColor);
		g.fillRect(0, 0, $w, $h);
		g.drawRenderedImage($image, null);
		g.dispose();
		return img;
	}

	/**
	 * 将32位色彩转换成24位色彩（丢弃Alpha通道）
	 *
	 * @param $argb
	 * @return
	 */
	public static int[] getRGBs(int[] $argb) {
		int[] __rgbs = new int[$argb.length];
		for (int i = 0; i < $argb.length; i++) {
			__rgbs[i] = $argb[i] & 0xFFFFFF;
		}
		return __rgbs;
	}

	public static void transferAlpha(String filePath) {
		InputStream is;
		try {
			File file = new File(filePath);
			is = new FileInputStream(file);
			// 如果是MultipartFile类型，那么自身也有转换成流的方法：is = file.getInputStream();
			BufferedImage bi = ImageIO.read(is);
			is.close();
			file.delete();
			Image image = (Image) bi;
			ImageIcon imageIcon = new ImageIcon(image);
			BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(),
					BufferedImage.TYPE_4BYTE_ABGR);
			Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
			g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
			int alpha = 0;
			for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
				for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
					int rgb = bufferedImage.getRGB(j2, j1);

					int R = (rgb & 0xff0000) >> 16;
					int G = (rgb & 0xff00) >> 8;
					int B = (rgb & 0xff);
					if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) {
						rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
					}

					bufferedImage.setRGB(j2, j1, rgb);

				}
			}

			g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
			ImageIO.write(bufferedImage, "png", new File(filePath));// 直接输出文件
			// deleteBlack(filePath.replace("png", "jpg"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 清除黑色
	 * 
	 * @param filePath
	 * @throws IOException
	 * @author dsq
	 * @date 2018年7月23日
	 */
	public static void deleteBlack(String filePath) throws IOException {
		File file = new File(filePath);
		Image ima = ImageIO.read(file);
		BufferedImage bufIma = new BufferedImage(ima.getWidth(null), ima.getHeight(null), BufferedImage.TYPE_INT_BGR);
		// 这里是关键部分
		Graphics2D g = bufIma.createGraphics();
		bufIma = g.getDeviceConfiguration().createCompatibleImage(ima.getWidth(null), ima.getHeight(null),
				Transparency.TRANSLUCENT);
		g = bufIma.createGraphics();
		g.drawImage(ima, 0, 0, null);
		file.delete();
		ImageIO.write(bufIma, "png", new File(filePath));
	}

}
